﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Gencode.PrsCommon.App.AppData;
using Gencode.PrsCommon.ControlData.GridControl;

namespace Gencode.PrsWeb.Controls.GridControls
{
	public partial class PlanGrid : System.Web.UI.UserControl
	{
		DataView PlanNames;

		protected void Page_Load(object sender, EventArgs e)
		{
			//if (!IsPostBack)
			//{
				string tmpPl = Request.QueryString["PLID"] as string;
				if (tmpPl != null && tmpPl != string.Empty)
				{
					PlanID = int.Parse(tmpPl);
					
					CreateGrid();
					//TODO:
					//Anzahl Dienste pro tag hinzufügen
				}
			//}
		}

		private void CreateGrid()
		{
			PlanGridGenerator gen = new PlanGridGenerator();
			gen.InitGrids(PlanID);
			PlanNames = gen.PlanNames;

			//NameGrid
			_nameGrid.DataSource = gen.NameTable;
			_nameGrid.DataBind();

			//PlanGrid
			_planGrid.DataSource = gen.DienstTable;
			_planGrid.DataBind();
			
			//SaldoGrid
			_saldoGrid.DataSource = gen.SaldoTabel;
			_saldoGrid.DataBind();

			GridColoring coloring = new GridColoring();
			coloring.PlanNames = gen.PlanNames;
			coloring.ColorGrid(_nameGrid, GridColoring.GridType.PlanPerson, planID);
			coloring.ColorGrid(_planGrid, GridColoring.GridType.PlanDienst, planID);
			coloring.ColorGrid(_saldoGrid, GridColoring.GridType.PlanSaldo, planID);
		}

		int planID;
		public int PlanID
		{
			get
			{
				return planID;
			}
			set
			{
				planID = value;
			}
		}

		protected void _planGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
		{
			if (e.Item.ItemIndex >= 0)
			{
				int prsID = int.Parse(PlanNames[e.Item.ItemIndex].Row[0].ToString());
				if (prsID > 0)
				{
					e.Item.Attributes.Add("Height", "10px");
					for (int i = 0; i < e.Item.Cells.Count; i++)
					{
						//CellClicked(prsID,abteilungID,day,planid,columnindex,rowindex)
						e.Item.Cells[i].Attributes.Add("ID", "R" + e.Item.ItemIndex + "C" + i);
						e.Item.Cells[i].Attributes.Add("OnClick", "CellClicked(" + prsID + ", "
							+ PlanNames[e.Item.ItemIndex].Row[3].ToString() + ", "
							+ (i + 1) + ", "
							+ PlanID + ", "
							+ i + ", "
							+ e.Item.ItemIndex
							+ ")");
						e.Item.Cells[i].Attributes.Add("title",
							PlanNames[e.Item.ItemIndex].Row["name"].ToString()
							+ " "
							+ PlanNames[e.Item.ItemIndex].Row["nachname"].ToString());
					}
				}
			}
		}

		protected void _saldoGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
		{
			if (e.Item.ItemIndex >= 0)
			{
				int prsID = int.Parse(PlanNames[e.Item.ItemIndex].Row[0].ToString());
				if (prsID > 0)
				{
					e.Item.Cells[0].Attributes.Add("ID", "V" + e.Item.ItemIndex);
					e.Item.Cells[1].Attributes.Add("ID", "S" + e.Item.ItemIndex);
					e.Item.Cells[2].Attributes.Add("ID", "H" + e.Item.ItemIndex);
					e.Item.Cells[3].Attributes.Add("ID", "D" + e.Item.ItemIndex);
				}
			}
		}
	}

	public class MyGrid : DataGrid
	{

	}
	public class GridArgs : System.EventArgs
	{
		public int PrsID
		{
			get;
			private set;
		}
		public int AbteilungID
		{
			get;
			private set;
		}
		public int Day
		{
			get;
			private set;
		}
		public int PlanID
		{
			get;
			private set;
		}

		public GridArgs(int prsID, int abtID, int day, int planID)
		{
			PrsID = planID;
			AbteilungID = abtID;
			Day = day;
			PlanID = planID;
		}
	}
}